package com.sysmanage.etl.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.etl.dao.CovertFactoryDao;
import com.sysmanage.etl.dao.DataSourceDao;
import com.sysmanage.etl.dao.PropertyDao;
import com.sysmanage.etl.dao.RuleDao;
import com.sysmanage.etl.dao.TableDao;
import com.sysmanage.etl.model.ETL_CovertFactory;
import com.sysmanage.etl.model.ETL_DataSource;
import com.sysmanage.etl.model.ETL_Property;
import com.sysmanage.etl.model.ETL_Rule;
import com.sysmanage.etl.model.ETL_Table;
import com.sysmanage.etl.service.IDataSourceService;
import com.sysmanage.etl.utils.PropertyUtils;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;

@Service
@Transactional
public class DataSourceServiceImpl extends BaseService<ETL_DataSource> implements IDataSourceService {
	@Resource
	private DataSourceDao dataSourceDao;
	@Resource
	private TableDao tableDao;
	@Resource
	private PropertyDao propertyDao;
	@Resource
	private CovertFactoryDao covertFactoryDao;
	@Resource
	private RuleDao ruleDao;

	@Override
	public List<ETL_DataSource> findList(int page, int count) {
		return dataSourceDao.findList("from ETL_DataSource", page, count);
	}

	@Override
	public ETL_DataSource queryDataSource(Integer id) {
		return dataSourceDao.findById(id);
	}

	@Override
	public void saveDataSource(ETL_DataSource ds) {
		List<ETL_Table> tables = PropertyUtils.getETL_Tables(ds);
		for (ETL_Table table : tables) {
			table.setDataSource(ds);
			ds.getTables().add(table);
			tableDao.save(table);
		}
		dataSourceDao.save(ds);
	}

	@Override
	public void updateDataSource(ETL_DataSource ds) {
		dataSourceDao.getHandler().alterObj(ds);
	}

	@Override
	public void deleteDataSource(Integer id) {
		// TODO:删除其他
		ETL_DataSource dataSource = dataSourceDao.findById(id);
		Set<ETL_Table> tables = dataSource.getTables();
		for (ETL_Table table : tables) {
			Set<ETL_Property> propertys = table.getPropertys();
			for (ETL_Property property : propertys) {

				ETL_CovertFactory covertFactory = covertFactoryDao
						.find("from ETL_CovertFactory where property.property_id = "
								+ property.getProperty_id());
				if (covertFactory != null) {
					covertFactoryDao.delete(covertFactory);
				}
				propertyDao.delete(property);
			}
			tableDao.delete(table);
		}
		dataSourceDao.delete(dataSource);
	}

	@Override
	public void reloadTable(ETL_DataSource dataSource, List<ETL_Table> tables) {
		if (tables == null || tables.size() <= 0) {
			return;
		}
		List<ETL_Table> havaTables = tableDao.findList("from ETL_Table t where t.dataSource.dataSource_id = " + dataSource.getDataSource_id());
		
		if (havaTables == null || havaTables.size() <= 0) {
			for ( ETL_Table table : tables ) {
				table.setDataSource(dataSource);
				tableDao.save(table);
			}
		} else {
			Map<String, ETL_Table> currentMap = new HashMap<String, ETL_Table>();
			Map<String, ETL_Table> haveMap = new HashMap<String, ETL_Table>();
			for (ETL_Table table : tables) {
				currentMap.put(table.getTable_name(), table);
			}
			
			for (ETL_Table table : havaTables) {
				haveMap.put(table.getTable_name(), table);
			}
			
			for (String tableName : currentMap.keySet()) {
				ETL_Table table = haveMap.get(tableName);
				if (table == null) {
					table = currentMap.get(tableName);
					table.setDataSource(dataSource);
					tableDao.save(table);
				}
			}
			
			for (String tableName : haveMap.keySet()) {
				ETL_Table table = currentMap.get(tableName);
				if (table == null) {
					table = haveMap.get(tableName);
					List<ETL_Rule> rules = ruleDao.findList("from ETL_Rule r where r.table.table_id = " + table.getTable_id());
					if (rules == null || rules.size() <= 0) {
						Set<ETL_Property> properties = table.getPropertys();
						for (ETL_Property property : properties) {
							covertFactoryDao.executeSql("delete from ETL_CovertFactory where property_id = " + property.getProperty_id());
							propertyDao.delete(property);
						}
						tableDao.delete(table);
					} else {
						table.setTable_exists(false);
						tableDao.alter(table);
					}
				}
			}
		}
	}

	@Override
	protected DAOInterface<ETL_DataSource> getDAO() {
		return dataSourceDao;
	}

}
